package com.spider.gp.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.dao.DieTing;
import com.spider.gp.dao.DieTingDao;
import com.spider.gp.dao.ShouBan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * description:
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class DieTingProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Autowired
    private DieTingDao dieTingDao;

    @Override
    public void process(Page page) {
//        String reportTime = formatter.format(new Date());
//        String reportTime = "2022-08-01";

        String s = page.getUrl().get();
        String reportTime = s.substring(s.length() - 8);

        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            Date date = formatter.parse(reportTime);
            SimpleDateFormat formatter01 = new SimpleDateFormat("yyyy-MM-dd");
            reportTime = formatter01.format(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


        LambdaQueryWrapper<DieTing> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DieTing::getReportTime, reportTime);
        dieTingDao.delete(wrapper);

        List<String> all = page.getJson().jsonPath("$.data.pool").all();
        for (String item : all) {
            HashMap hashMap = JSON.parseObject(item, HashMap.class);
            String days = hashMap.get("days").toString();//连板
            hashMap.get("amount");//成交额
            String code = hashMap.get("c").toString();//code
            String name = hashMap.get("n").toString();//股票名称
            String hs = new BigDecimal(hashMap.get("hs").toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();//换手
            String zdp = new BigDecimal(hashMap.get("zdp").toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();//换手 hashMap.get("zdp").toString();//涨跌幅
            String hybk = hashMap.get("hybk").toString();//行业板块
            String m = hashMap.get("m").toString();//0 深圳 1：上证
            String p = new BigDecimal(hashMap.get("p").toString()).divide(new BigDecimal(1000)).toString(); //最新价hashMap.get("p").toString();
            String ltsz = new BigDecimal(hashMap.get("ltsz").toString()).divide(new BigDecimal(100000000)).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "亿"; //流通市值 hashMap.get("ltsz").toString();
            String oc = hashMap.get("oc").toString();//开板次数
            String fund = new BigDecimal(hashMap.get("fund").toString()).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "万";//封板资金
            String fba = new BigDecimal(hashMap.get("fba").toString()).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "万";//板上封板资金
            String lbt = hashMap.get("lbt").toString();//最后封板时间
            String pe = new BigDecimal(hashMap.get("pe").toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();//市盈率
            String tshare = new BigDecimal(hashMap.get("tshare").toString()).divide(new BigDecimal(100000000)).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "亿";//总市值 hashMap.get("tshare").toString();

            DieTing ban = DieTing.builder().reportTime(reportTime).code(code).name(name).hs(hs).zdf(zdp).hybk(hybk).lbc(days)
                    .m(m).price(p).ltsz(ltsz).oc(oc).fund(fund).fba(fba).lbt(lbt).tshare(tshare).pe(pe).build();
            dieTingDao.insert(ban);

        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}








